package cn.longwingstech.intelligence.agent.monitor;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class AiModelMetricsCollector {
    @Resource
    private MeterRegistry meterRegistry;


    public ConcurrentMap<String, Counter> recordMap = new ConcurrentHashMap<>();
    private final ConcurrentMap<String, Timer> responseTimersCache = new ConcurrentHashMap<>();

    /**
     * 响应时间
     * @param metName
     * @param duration
     */
    public void recordRequestResponseTime(String metName, Duration duration) {
        String key = "key_" + metName+ "_time";
        Timer timer = responseTimersCache.computeIfAbsent(key,k->{
            return Timer.builder("agent_request_response_time")
                    .description("Request response time")
                    .tag("method", metName)
                    .register(meterRegistry);
        });
        timer.record(duration);
    }

    /**
     * 接口请求次数
     *
     * @param metName 请求方法名
     */
    public void recordRequest(String metName) {
        String key = "key_" + metName;
        Counter counter = recordMap.computeIfAbsent(key, k ->
                Counter.builder("agent_request_count")
                        .tag("name", metName)
                        .register(meterRegistry)
        );
        counter.increment();
    }

    /**
     * 请求报错接口请求次数
     *
     * @param metName 请求方法名
     */
    public void recordErrorRequest(String metName) {
        String key = "key_error_" + metName;
        Counter counter = recordMap.computeIfAbsent(key, k ->
                Counter.builder("agent_error_request_count")
                        .tag("name", metName)
                        .register(meterRegistry)
        );
        counter.increment();
    }

    /**
     * 模型请求token消耗记录
     * @param modelName
     * @param type
     * @param tokenCount
     */
    public void recordAllModelToken(String modelName,String type,long tokenCount) {
        String key = "key_all_"+ modelName+"_" + type;
        Counter counter = recordMap.computeIfAbsent(key, k ->
                Counter.builder("agent_all_model_request_token_count")
                        .tag("modelName", modelName)
                        .tag("type", type)
                        .register(meterRegistry)
        );
        counter.increment(tokenCount);
    }

    /**
     * 模型请求token消耗记录
     * @param userId
     * @param modelName
     * @param type
     * @param tokenCount
     */
    public void recordAllModelToken(String userId,String modelName,String type,long tokenCount) {
        String key = "key_"+userId+"_"+ modelName+"_"+type;
        Counter counter = recordMap.computeIfAbsent(key, k ->
                Counter.builder("agent_model_request_token_count")
                        .tag("userId", userId)
                        .tag("modelName", modelName)
                        .tag("type", type)
                        .register(meterRegistry)
        );
        counter.increment(tokenCount);
    }
}
